<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./login.css" />
    <script src="../js/ajax.js"></script>
    <script src="../cookie/cookie.js"></script>
    <title>Document</title>
  </head>
  <body>
    <div class="loginContainer">
      <h1>登录</h1>
      <form action="/checkUser" method="post" onsubmit="return false;">
        姓名：
        <input
          class="inputStyle1 inputStyle username"
          type="text"
          name="username"
        />
        <span class="exchange">用户名不正确</span>
        <br />密码：
        <input class="inputStyle pwd" type="password" name="pwd" /><br />
        记住我 <input type="checkbox" class="remeber" />
        <input class="loginStyle" type="submit" value="登录" />
      </form>
    </div>
    <script>
      let usernameEle = document.querySelector(".username");
      let pwdEle = document.querySelector(".pwd");
      let login = document.querySelector(".loginStyle");
      let remeber = document.querySelector(".remeber");
      login.onclick = function () {
        let username = usernameEle.value;
        let pwd = pwdEle.value;
        ajax({
          url: "checkuser.php",
          method: "post",
          data: {
            username,
            pwd,
          },
          dataType: "json",
          success(res) {
            if (res.status == 1) {
              //   console.log(res);
              //   setCookie("username", res.data.username, 0);
              //   setCookie("uid", res.data.id, 0);
              document.cookie = "username=" + res.data.username;
              document.cookie = "uid=" + res.data.id;
            }
            if (remeber.checked) {
              setCookie("username", res.data.username, 3600 * 24 * 7);
              setCookie("uid", res.data.id, 3600 * 24 * 7);
            }
            window.location.href = "./shop/shopcar.html";
          },
        });
      };
    </script>
  </body>
</html>
